﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using System.Linq;
using System.Text;

namespace eCommerce.DataAccess
{
    class CartProduct
    {
        #region Properties
	public Guid CartID { get; set; }
	public Int32 ProductID { get; set; }
	public Int32 ProductType { get; set; }
	public Int32 Quantity { get; set; }
	public String Custom1 { get; set; }
	public String Custom2 { get; set; }
	public String Custom3 { get; set; }
	public Double PriceGBP { get; set; }
	public Double PriceUSD { get; set; }
	public Double Discount { get; set; }
	public Boolean Exists { get; set; }
	#endregion

	#region Constructors
	public CartProduct(Guid cartid, Int32 productid, Int32 producttype)
	{
		Get(cartid, productid, producttype);
	}
	#endregion

	private void Get(Guid cartid, Int32 productid, Int32 producttype)
	{

        eCommerceDataContext dataContext = new eCommerceDataContext();
        var cart = (from cr in dataContext.custom_getcartproduct(cartid, productid, producttype)
                                    select cr).FirstOrDefault();
                 if (cart != null)
                 {
                     CartID = new Guid(cart.cartid.ToString());
                     ProductID = Int32.Parse(cart.productid.ToString());
                     ProductType = Int32.Parse(cart.producttype.ToString());
                     Quantity = Int32.Parse(cart.qty.ToString());
                     //Custom3 = cart.custom3.ToString();

                     //Discount = Double.Parse(cart.discount.ToString());
                     ////switch (ProductType)
                     ////{
                     ////    case 1:
                     ////        //PriceGBP = new ReportLicense(ProductID).PriceGBP;
                     ////        //PriceUSD = new ReportLicense(ProductID).PriceUSD;
                     ////        break;
                     ////    case 2:
                     ////        //PriceGBP = new ConferenceItem(ProductID).PriceGBP;
                     ////        //PriceUSD = new ConferenceItem(ProductID).PriceUSD;
                     ////        break;
                     ////    default:
                     ////        PriceGBP = 0;
                     ////        PriceUSD = 0;
                     ////        break;
                     ////}
                     Exists = true;
                 }
                else
                 {
                     Exists = false;
                 }
        
        
	}
   
	public static void Add(Guid cartid, Int32 productid, Int32 producttype, Int32 qty)
	{
		CartProduct x = new CartProduct(cartid, productid, producttype);
		if (x.Exists)
		{
			UpdateAdd(cartid, productid, producttype);
		}
		else
		{
			Insert(cartid, productid, producttype, qty);
		}
	}

    private static void Insert(Guid cartid, Int32 productid, Int32 producttype, Int32 qty)
	{
	    using (eCommerceDataContext ndc = new eCommerceDataContext())
	    {

            Cart_Product cart = new Cart_Product
                                    {
                                        productid = productid,
                                        producttype = producttype,
                                        cartid = cartid,
                                        qty = qty,
                                        datetime = Convert.ToDateTime(DateTime.Now.ToShortDateString())
                                    };

	        ndc.Cart_Products.InsertOnSubmit(cart);
                ndc.SubmitChanges();
            
	    }
	}

     
    private static void UpdateAdd(Guid cartid, Int32 productid, Int32 producttype)
	{
        using (eCommerceDataContext ndc = new eCommerceDataContext())
        {
            var qtynow = (from cr in ndc.Cart_Products
                          where cr.cartid == cartid && cr.productid == productid && cr.producttype == producttype
                          select cr).FirstOrDefault();
            var qtyF = qtynow.qty + 1;
            Cart_Product cart = new Cart_Product
                                    {
                                        productid = productid,
                                        producttype = producttype,
                                        cartid = cartid,
                                        qty = qtyF,
                datetime = Convert.ToDateTime(DateTime.Now.ToShortDateString())
            };

            ndc.SubmitChanges();
                        
        }


	    //using (SqlConnection sqlConn = new SqlConnection(ConfigurationManager.ConnectionStrings["SqlConnectionString"].ConnectionString))
        //{
        //    sqlConn.Open();
        //    SqlCommand sqlcmd = new SqlCommand("custom_updatecartproductadd", sqlConn);
        //    sqlcmd.CommandType = CommandType.StoredProcedure;
        //    sqlcmd.Parameters.Add(new SqlParameter("@cartid", cartid));
        //    sqlcmd.Parameters.Add(new SqlParameter("@productid", ProductID));
        //    sqlcmd.Parameters.Add(new SqlParameter("@producttype", ProductType));
        //    Int32 rowcount = sqlcmd.ExecuteNonQuery();
        //}
	}

    //public static void UpdateDiscount(Guid cartid, Int32 ProductID, Int32 ProductType, Double Discount)
    //{
    //    using (SqlConnection sqlConn = new SqlConnection(ConfigurationManager.ConnectionStrings["SqlConnectionString"].ConnectionString))
    //    {
    //        sqlConn.Open();
    //        SqlCommand sqlcmd = new SqlCommand("custom_updatecartproductdiscount", sqlConn);
    //        sqlcmd.CommandType = CommandType.StoredProcedure;
    //        sqlcmd.Parameters.Add(new SqlParameter("@cartid", cartid));
    //        sqlcmd.Parameters.Add(new SqlParameter("@productid", ProductID));
    //        sqlcmd.Parameters.Add(new SqlParameter("@producttype", ProductType));
    //        sqlcmd.Parameters.Add(new SqlParameter("@discount", Discount));
    //        Int32 rowcount = sqlcmd.ExecuteNonQuery();
    //    }
    //}
    }
}
